﻿var m_hkbah = '';
var m_project_id = "";
var m_cksqdj = null;
$(document).ready(function () {
	//创建档案
    CreateDAXM();
    loadData();
    //保存封面信息
    $("#btnSave").click(function () {
        m_cksqdj.SetValue("PROJECT_ID", m_project_id);
        $(":text").each(function () {
            m_cksqdj.SetValue($(this).attr("name"), $(this).val());
        });
        if (m_hkbah != '') {
            var keyfields = "申请人,矿山名称,填表人,填表时间";
            var keyvalues = $("#txt申请人").val() + "," + $("#txt矿山名称").val() + "," + $("#txt填表人").val() + "," + $("#txt填表时间").val();
            var data = "{'project_id':'" + m_project_id + "','keyfields':'" + keyfields + "','keyvalues':'" + keyvalues + "','hkbah':'" + m_hkbah + "'}";
            GetSmJSON(DBCenterServiceUrl + "/KZ_SPDJ/KZ_SPDJ_CKSQDJ.asmx/SaveCKQXLFROMHDKQFW", data, success);
        }
        else {
            //            var res = m_cksqdj.Update();
            var res = m_cksqdj.Save(DBCenterServiceUrl + "/KZ_SPDJ/KZ_SPDJ_CKSQDJ.asmx/SaveCKQXL", "'LX':'FM'");
            alertex("保存成功",true);            
        }
        function success(obj) {
            alertex2(obj);
        }
    });
});
function loadData() {
    form = document.getElementById("form1");
    form.encoding = "multipart/form-data";
    //给时间框添加时间选择器
    $("#txt填表时间").click(function () {
        WdatePicker({ dateFmt: 'yyyy-M-d' });
    });
    //定义一个对象
    m_cksqdj = new SmObjectData("CKQDJK", "采矿申请登记");

    //加载一条项目记录
    m_project_id = $.query.get("Project_ID");
    var SLLX = $.query.get("SLLX");
    if (SLLX != "1") {
        $(".m_btn").css("display", "none");
    }
    $("#id_project_id").val(m_project_id);
    m_cksqdj.GetFirstSmObject("PROJECT_ID=\\\'" + m_project_id + "\\\'");
    $(":text").each(function () {
        var ff = $(this).attr("name")
        $(this).val(m_cksqdj.GetValue(ff));
    });
}

function InitDJSFromExistPrj(objstr) {
    var obj = JSON.parse(objstr);
    if (obj != null && obj.Data != null && obj.Data.length > 0) {
        for (i = 0; i < obj.Data.length; i++) {
            var key = obj.Data[i].Key;
            if (key == "申请人" || key == "矿山名称" || key == "填表时间" || key == "填表人") {
                $("input[name='" + key + "']").val(obj.Data[i].Value);
            }
        }
        if ($("#txt填表时间").val() == "") {
            $("#txt填表时间").val(new Date().getFullYear() + "-" + parseInt(new Date().getMonth() + 1) + "-" + new Date().getDate());
        }
    }
}

function selHDKQFWDJXM() {
    //选择的是项目类型为1010（办结）并且项目状态为0（位被选中）的划矿项目
    var sel = new selDJXM();
    sel.Show(selDJXMCallBack);
}

function selDJXMCallBack(obj) {
    //    m_hkbah = obj.划矿备案号;
    //    $("#txtSQR").val(obj.申请人);
    //    $("#txt矿山名称").val(obj.矿区名称);
    //    $("#txt填表人").val(obj.填表人);
    //alertex(obj);
    copy(obj, funcallback);
    function funcallback(obj) {
        var v = obj.d;
        if (v == "") {
            alertex("项目未选择成功", true);
            return;
        }
        //m_cksqdj.GetFirstSmObject("PROJECT_ID=\\\'" + m_project_id + "\\\'");
        var dbalias = "CKQDJK";
        var tablename = "采矿申请登记";
        var condittion = "BSM=\\\'" + v + "\\\'";
        GetFirstSmObject(dbalias, tablename, condittion, InitDJSFromExistPrj);
        loadData();
    }
}

function copy(xkzh, funcallback) {
    $.ajax({
        url: DBCenterServiceUrl + "/KZ_SPDJ/KZ_SPDJ_CKQYX.asmx/CopyCKQXL",
        type: "POST",
        dataType: "json",
        data: "{'project_id':'" + m_project_id + "','xkzh':'" + xkzh + "'}",
        contentType: "application/json; charset=utf-8",
        success: function (json) {
            if (funcallback != null) {
                funcallback(json);
            }
        },
        error: function (x, e) {
        },
        complete: function (x) {
        }
    });
}